//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "ForwardRate.h"
using namespace Cephei::QL::Termstructures::Yield;
#include <gen/QL/Termstructures/YieldTermStructure.h>
using namespace Cephei::QL::Termstructures;
#undef HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::Termstructures::Yield::CForwardRate::CForwardRate (boost::shared_ptr<QuantLib::ForwardRate>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phForwardRate = NULL;
#endif
	_ppForwardRate = &childNative;
    
}
Cephei::QL::Termstructures::Yield::CForwardRate::CForwardRate (QuantLib::ForwardRate& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phForwardRate = NULL;
#endif
	_ppForwardRate = new boost::shared_ptr<QuantLib::ForwardRate> (&childNative);
    
    _ForwardRateOwner = owner;
    
}

Cephei::QL::Termstructures::Yield::CForwardRate::CForwardRate (CForwardRate^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phForwardRate = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppForwardRate = new boost::shared_ptr<QuantLib::ForwardRate> (copy->GetShared());
        
    }
}
Cephei::QL::Termstructures::Yield::CForwardRate::CForwardRate (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phForwardRate = NULL;
#endif
	if (!t->IsSubclassOf(CForwardRate::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Yield::CForwardRate::CForwardRate (QuantLib::Handle<QuantLib::ForwardRate>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phForwardRate = &childNative;
	_ppForwardRate = &static_cast<boost::shared_ptr<QuantLib::ForwardRate>>(childNative.currentLink());
    
    _ForwardRateOwner = owner;
}
Cephei::QL::Termstructures::Yield::CForwardRate::CForwardRate (QuantLib::Handle<QuantLib::ForwardRate> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phForwardRate = &childNative;
	_ppForwardRate = &static_cast<boost::shared_ptr<QuantLib::ForwardRate>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Yield::CForwardRate::CForwardRate (QuantLib::ForwardRate childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phForwardRate = NULL;
#endif
	_ppForwardRate = new boost::shared_ptr<QuantLib::ForwardRate> (new QuantLib::ForwardRate (childNative));
    
}
#endif

Cephei::QL::Termstructures::Yield::CForwardRate::~CForwardRate ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppForwardRate != NULL)
    {
	    delete _ppForwardRate;
        _ppForwardRate = NULL;
    }
}
Cephei::QL::Termstructures::Yield::CForwardRate::!CForwardRate ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppForwardRate != NULL)
    {
	    delete _ppForwardRate;
    }
}
QuantLib::ForwardRate& Cephei::QL::Termstructures::Yield::CForwardRate::GetReference ()
{
    if (_ppForwardRate == NULL) throw gcnew NativeNullException ();
	return **_ppForwardRate;
}
boost::shared_ptr<QuantLib::ForwardRate>& Cephei::QL::Termstructures::Yield::CForwardRate::GetShared ()
{
    if (_ppForwardRate == NULL) throw gcnew NativeNullException ();
	return *_ppForwardRate;
}
QuantLib::ForwardRate* Cephei::QL::Termstructures::Yield::CForwardRate::GetPointer ()
{
    if (_ppForwardRate == NULL) throw gcnew NativeNullException ();
	return &**_ppForwardRate;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ForwardRate>& Cephei::QL::Termstructures::Yield::CForwardRate::GetHandle ()
{
	if (_phForwardRate == NULL)
	{
		_phForwardRate = new Handle<QuantLib::ForwardRate> (*_ppForwardRate);
	}
	return *_phForwardRate;
}
#endif
bool Cephei::QL::Termstructures::Yield::CForwardRate::HasNative () 
{
	return (_ppForwardRate != NULL);
}

Boolean Cephei::QL::Termstructures::Yield::CForwardRate::DummyInitialValue::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	bool _rv = (bool)(*_ppForwardRate)->dummyInitialValue ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Yield::CForwardRate::Guess (Cephei::QL::Termstructures::IYieldTermStructure^ c, DateTime d)
{
    CYieldTermStructure^ _Cc;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cc = safe_cast<CYieldTermStructure^> (c);
        _Cc->Lock();
        QuantLib::YieldTermStructure* _c = static_cast<QuantLib::YieldTermStructure*> (_Cc->GetPointer ()); 
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d);
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppForwardRate)->guess ( _c,  _d );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cc != nullptr) _Cc->Unlock();
    }
}
DateTime Cephei::QL::Termstructures::Yield::CForwardRate::InitialDate (Cephei::QL::Termstructures::IYieldTermStructure^ c)
{
    CYieldTermStructure^ _Cc;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cc = safe_cast<CYieldTermStructure^> (c);
        _Cc->Lock();
        QuantLib::YieldTermStructure* _c = static_cast<QuantLib::YieldTermStructure*> (_Cc->GetPointer ()); 
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppForwardRate)->initialDate ( _c );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cc != nullptr) _Cc->Unlock();
    }
}
Double Cephei::QL::Termstructures::Yield::CForwardRate::InitialGuess::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppForwardRate)->initialGuess ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Yield::CForwardRate::InitialValue (Cephei::QL::Termstructures::IYieldTermStructure^ prm1)
{
    CYieldTermStructure^ _Cprm1;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cprm1 = safe_cast<CYieldTermStructure^> (prm1);
        _Cprm1->Lock();
        QuantLib::YieldTermStructure* _prm1 = static_cast<QuantLib::YieldTermStructure*> (_Cprm1->GetPointer ()); 
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppForwardRate)->initialValue ( _prm1 );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
    }
}
UInt64 Cephei::QL::Termstructures::Yield::CForwardRate::MaxIterations::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppForwardRate)->maxIterations ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Yield::CForwardRate::MaxValueAfter (UInt64 prm1, Cephei::IVector<Double>^ prm2)
{
    CoVector<Double>^ _Cprm2;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Size _prm1 = (QuantLib::Size)ValueHelper::Convert (prm1);
        _Cprm2 = safe_cast<CoVector<Double>^> (prm2);
        _Cprm2->Lock();
        INativeVector<Double>^ _NCIprm2 = _Cprm2->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCprm2 = safe_cast<CDoubleVector^>(_NCIprm2);
        std::vector<QuantLib::Real>& _prm2 = static_cast<std::vector<QuantLib::Real>&> (_NCprm2->GetReference ());
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppForwardRate)->maxValueAfter ( _prm1,  _prm2 );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm2 != nullptr) _Cprm2->Unlock();
    }
}
Double Cephei::QL::Termstructures::Yield::CForwardRate::MinValueAfter (UInt64 prm1, Cephei::IVector<Double>^ prm2)
{
    CoVector<Double>^ _Cprm2;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Size _prm1 = (QuantLib::Size)ValueHelper::Convert (prm1);
        _Cprm2 = safe_cast<CoVector<Double>^> (prm2);
        _Cprm2->Lock();
        INativeVector<Double>^ _NCIprm2 = _Cprm2->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCprm2 = safe_cast<CDoubleVector^>(_NCIprm2);
        std::vector<QuantLib::Real>& _prm2 = static_cast<std::vector<QuantLib::Real>&> (_NCprm2->GetReference ());
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppForwardRate)->minValueAfter ( _prm1,  _prm2 );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm2 != nullptr) _Cprm2->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::IForwardRate^ Cephei::QL::Termstructures::Yield::CForwardRate::UpdateGuess (Cephei::IVector<Double>^ data, Double forward, UInt64 i)
{
    CoVector<Double>^ _Cdata;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cdata = safe_cast<CoVector<Double>^> (data);
        _Cdata->Lock();
        INativeVector<Double>^ _NCIdata = _Cdata->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCdata = safe_cast<CDoubleVector^>(_NCIdata);
        std::vector<QuantLib::Rate>& _data = static_cast<std::vector<QuantLib::Rate>&> (_NCdata->GetReference ());
        QuantLib::Rate _forward = (QuantLib::Rate)ValueHelper::Convert (forward);
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i);
    	(*_ppForwardRate)->updateGuess ( _data,  _forward,  _i );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cdata != nullptr) _Cdata->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

